package org.mybatis3.demo1;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis3.demo1.pojo.User;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test2 {
    public static void main(String[] args) {

        InputStream is=null;
        SqlSession sqlSession=null;


        try {
            is= Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
            sqlSession=sqlSessionFactory.openSession();

            User user1=new User();
            user1.setUserCode("tlp");
            user1.setUserName("总统");
            user1.setUserPassword("123");
            user1.setGender(2);
            SimpleDateFormat fmt=new SimpleDateFormat("yyyy-MM-dd");
            Date date1=fmt.parse("1968-10-10");
            user1.setBirthday(date1);
            user1.setPhone("15978546589");
            user1.setAddress("北京路20号");
            user1.setUserRole(1L);
            user1.setCreatedBy(1L);
            user1.setCreationDate(new Date());
            if(sqlSession.insert("org.mybatis3.demo1.dao.UserMapper.save",user1)>0){
                System.out.println("新增数据成功！");
            }

            sqlSession.commit();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }finally {
            if(sqlSession!=null){
                sqlSession.close();
            }
        }


    }
}
